<?php namespace App\Http\Controllers\Api\Admin;

use App\Admin;
use App\Banner;

use App\BuildingInfos;
use App\BuildingPartyBranch;
use App\BuildingProperty;
use App\BuildingPublicity;
use App\BuildingRecruit;
use App\Building;
use App\BuildingCompany;
use App\Classify;
use App\Content;
use App\DakaContent;
use App\Http\Controllers\Controller;
use App\News;
use App\Questions;
use App\QuestionsAnswers;
use App\QuestionsPackage;
use App\RedInfo;
use App\RedInfoImgs;
use App\SmallDevices;
use App\SmallVideo;
use App\User;
use App\UserDevice;
use App\Userinfo;
use App\VideoData;
use App\VideoModel;
use Auth;
use EasyWeChat\Device\Device;
use Illuminate\Http\Request;

class SystemController extends Controller
{
    public function setBanner (Request $request)
    {
        if (Auth::guard("admin")->guest())
            return response()->json(['level' => 'error', 'message' => '请先登录！']);

        $title = $request->get('title');
        $linkUrl = $request->get('link_url');
        $sort = $request->get('sort');
        $imgId = $request->get('img_id');
        $id = !is_null($request->get('id')) ? $request->get('id') : 0;

        $thatBanner = Banner::whereId(trim($id))->first();

        if (!$thatBanner)
            $thatBanner = new Banner();

        if (!is_null($title)) $thatBanner->title = $title;
        if (!is_null($linkUrl)) $thatBanner->link_url = $linkUrl;
        if (!is_null($sort)) $thatBanner->sort = $sort;
        if (!is_null($imgId)) $thatBanner->img_id = $imgId;
        $thatBanner->save();

        return response()->json(["level" => "success", "message" => "添加或修改成功", "data" => $thatBanner]);
    }

    public function delBanner (Request $request)
    {
        if (Auth::guard("admin")->guest())
            return response()->json(['level' => 'error', 'message' => '请先登录！']);

        $id = !is_null($request->get('id')) ? $request->get('id') : 0;

        /** @var Banner $thatBanner */
        $thatBanner = Banner::whereId($id)->first();

        if (!$thatBanner)
            return response()->json(["level" => "error", "message" => "该banner不存在！"]);

        $thatBanner->delete();

        return response()->json(["level" => "success", "message" => "删除成功", "data" => $thatBanner]);
    }

    public function setAdmin (Request $request)
    {
        if (Auth::guard("admin")->guest())
            return response()->json(['level' => 'error', 'message' => '请先登录！']);

        $username = $request->get('username');
        $password = $request->get('password');
        $repass = $request->get('repass');

        if ($password != $repass)
            return response()->json(['level' => 'error', 'message' => '两次密码不一致！']);

        /** @var Admin $hasUser */
        $hasUser = Admin::whereUsername($username)->first();
        if ($hasUser) {
            return response()->json(['level' => 'error', 'message' => '当前管理员账号已存在']);
        }

        $thatAdmin = new Admin();
        $thatAdmin->username = $username;
        $thatAdmin->password = bcrypt($password);
        $thatAdmin->status = 2;
        $thatAdmin->save();

        return response()->json(['level' => 'success', 'message' => '添加成功', 'data' => $thatAdmin]);
    }

    public function setUser (Request $request)
    {

        if (Auth::guard("admin")->guest())
            return response()->json(['level' => 'error', 'message' => '请先登录！']);

        $username = $request->get('username');
        $password = $request->get('password');
        $repass = $request->get('repass');

        if ($password != $repass)
            return response()->json(['level' => 'error', 'message' => '两次密码不一致！']);

        /** @var User $hasUser */
        $hasUser = User::whereUsername($username)->first();
        if ($hasUser) {
            return response()->json(['level' => 'error', 'message' => '当前管理员账号已存在']);
        }

        $thatUser = new User();
        $thatUser->username = $username;
        $thatUser->password = bcrypt($password);
        $thatUser->role = 1;
        $thatUser->status = 1;
        $thatUser->save();

        $thatUserInfo = new Userinfo();
        $thatUserInfo->user_id = $thatUser->id;

        $thatUserInfo->save();

        return response()->json(['level' => 'success', 'message' => '添加成功', 'data' => $thatUser]);


    }

    public function delAdmin (Request $request)
    {
        if (Auth::guard("admin")->guest())
            return response()->json(['level' => 'error', 'message' => '请先登录！']);

        $id = !is_null($request->get('id')) ? $request->get('id') : 0;

        /** @var Admin $thatAdmin */
        $thatAdmin = Admin::whereId($id)->first();

        if (!$thatAdmin)
            return response()->json(["level" => "error", "message" => "该管理员不存在！"]);

        if ($thatAdmin->status == 1)
            return response()->json(["level" => "error", "message" => "该管理员不能删除！"]);


        $thatAdmin->forceDelete();

        return response()->json(["level" => "success", "message" => "删除成功", "data" => $thatAdmin]);
    }

    public function bindingDeviceNum (Request $request)
    {
        if (Auth::guard('admin')->guest())
            return response()->json(['level' => 'error', 'message' => '请先登录!']);

        $deviceNum = $request->get('device_num');
        $id = !is_null($request->get('id')) ? $request->get('id') : 0;

        /** @var DakaContent $dakaContent */
        $dakaContent = DakaContent::whereId($id)->first();
        if (!$dakaContent)
            return response()->json(['level' => 'error', 'message' => '该打卡任务不存在']);

        if (!is_null($deviceNum) && $deviceNum != '') $dakaContent->device_num = $deviceNum;
        $dakaContent->save();

        return response()->json(['level' => 'success', 'message' => '绑定成功', 'data' => $dakaContent]);
    }

    public function setDevice (Request $request)
    {

        $device_num = $request->get('device_num');
        $desc = $request->get('desc');
        $info = $request->get('info');
        $province = $request->get('province');
        $city = $request->get('city');
        $area = $request->get('area');
        $id = !is_null($request->get('id')) ? $request->get('id') : 0;

        if (Auth::guard('admin')->guest())
            return response()->json(['level' => 'error', 'message' => '请先登录!']);


        /** @var Device $device */
        $device = SmallDevices::whereId($id)->first();
        if (!$device)
            $device = new SmallDevices();
        if (!is_null($device_num)) $device->device_num = $device_num;
        if (!is_null($desc)) $device->desc = $desc;
        if (!is_null($info)) $device->info = $info;
        if (!is_null($province)) $device->province = $province;
        if (!is_null($city)) $device->city = $city;
        if (!is_null($area)) $device->area = $area;

        $device->save();

        return response()->json(['level' => 'success', 'message' => '设置成功!','data'=>$device]);
    }

    public  function  deleteDevice(Request $request){
        $id = $request->get('id');

        if (Auth::guard('admin')->guest())
            return response()->json(['level' => 'error', 'message' => '请先登录!']);

        $device = SmallDevices::whereId($id)->first();
        if (!$device)
            return response()->json(['level' => 'error', 'message' => '设备不存在!']);

        $device->delete();

        return response()->json(['level' => 'success', 'message' => '删除成功!','data'=>$device]);

    }

    public  function  delUser(Request $request){
        $id = $request->get('id');
        if (Auth::guard('admin')->guest())
            return response()->json(['level' => 'error', 'message' => '请先登录!']);
        /** @var User $user */
        $user=User::whereId($id)->whereRole(1)->first();
        if(!$user)
            return response()->json(['level' => 'error', 'message' => '用户不存在!']);

        $user->delete();
        return response()->json(['level' => 'success', 'message' => '删除成功!','data'=>$user]);
    }

    public function setContents(Request $request){
        if(Auth::guard('admin')->guest()){
            return response()->json(["level"=>"error","message"=>"不允许访问"]);
        }

        $id       = !is_null($request->get('id')) ? $request->get('id') : 0;
        $title    = $request->get("title");
        $content  = $request->get("content");
        $desc     = $request->get("desc");
        $linkUrl  = $request->get("link_url");
        $imgId    = $request->get("img_id");
        $classify = $request->get("classify");

        /** @var Content $thatContents */
        $thatContents = Content::whereId($id)->first();

        if(!$thatContents){
            $thatContents = new Content();
        }

        if(!is_null($title))$thatContents->title = $title;
        if(!is_null($content))$thatContents->content = $content;
        if(!is_null($desc))$thatContents->desc = $desc;
        if(!is_null($linkUrl))$thatContents->link_url = $linkUrl;
        if(!is_null($imgId))$thatContents->img_id = $imgId;
        if(!is_null($classify))$thatContents->classify_id = $classify;

        $thatContents->save();

        return response()->json(["level"=>"success","message"=>"设置完成","data"=>$thatContents]);
    }

    public function delContents(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $id       = !is_null($request->get('id')) ? $request->get('id') : 0;

        $newContents = Content::whereId($id)->first();

        if(!$newContents)
            return response()->json(['level'=>'error','message' => '没有该文章！']);

        $newContents->delete();

        return response()->json(["level"=>"success","message"=>"删除完成","data"=>$newContents]);
    }

    public function setBuildingRecruit(Request $request){
        if(Auth::guard('admin')->guest())
            return response()->json(['level' => 'error','message' => '请先登录!']);

        $id           = !is_null($request->get('id')) ? $request->get('id') : 0;
        $buildingId   = $request->get("building_id");
        $companyName  = $request->get("company_name");
        $position     = $request->get("position");
        $workAddress  = $request->get("work_address");
        $num          = $request->get("num");

        /** @var BuildingRecruit $thatBuildingRecruit */
        $thatBuildingRecruit = BuildingRecruit::whereId($id)->first();

        if(!$thatBuildingRecruit){
            $thatBuildingRecruit = new BuildingRecruit();
        }

        if(!is_null($buildingId))$thatBuildingRecruit->building_id = $buildingId;
        if(!is_null($companyName))$thatBuildingRecruit->company_name = $companyName;
        if(!is_null($position))$thatBuildingRecruit->position = $position;
        if(!is_null($workAddress))$thatBuildingRecruit->work_address = $workAddress;
        if(!is_null($num))$thatBuildingRecruit->num = $num;
        $thatBuildingRecruit->save();

        return response()->json(['level' => 'success','message' => '设置成功','data' => $thatBuildingRecruit]);
    }

    public function delBuildingRecruit(Request $request){
        if(Auth::guard('admin')->guest())
            return response()->json(['level' => 'error','message' => '请先登录!']);

        $id = !is_null($request->get('id'))?$request->get('id'):0;

        $thatBuildingRecruit =BuildingRecruit::whereId($id)->first();

        if(!$thatBuildingRecruit)
            return response()->json(["level"=>"error","message"=>"该招聘信息不存在！"]);

        $thatBuildingRecruit->delete();

        return response()->json(["level"=>"success","message"=>"删除成功","data"=>$thatBuildingRecruit]);
    }

    public function setBuildingActivity(Request $request){
        if(Auth::guard('admin')->guest())
            return response()->json(['level' => 'error','message' => '请先登录!']);

        $id                = !is_null($request->get('id')) ? $request->get('id') : 0;
        $title             = $request->get("title");
        $desc              = $request->get("desc");
        $content           = $request->get("content");
        $isShow            = $request->get("is_show");
        $buildingId        = $request->get("building_id");
        $type              = $request->get('type');

        /** @var BuildingPublicity $thatBuildingActivity */
        $thatBuildingActivity = BuildingPublicity::whereId($id)->first();

        if(!$thatBuildingActivity){
            $thatBuildingActivity = new BuildingPublicity();
        }

        if(!is_null($title))$thatBuildingActivity->title = $title;
        if(!is_null($desc))$thatBuildingActivity->desc = $desc;
        if(!is_null($content))$thatBuildingActivity->content = $content;
        if(!is_null($isShow))$thatBuildingActivity->is_show = $isShow;
        if(!is_null($buildingId))$thatBuildingActivity->building_id = $buildingId;
        if(!is_null($type))$thatBuildingActivity->type = $type;
        $thatBuildingActivity->save();

        return response()->json(['level' => 'success','message' => '设置成功','data' => $thatBuildingActivity]);
    }

    public function delBuildingActivity(Request $request){
        if(Auth::guard('admin')->guest())
            return response()->json(['level' => 'error','message' => '请先登录!']);

        $id = !is_null($request->get('id'))?$request->get('id'):0;

        $thatBuildingActivity =BuildingPublicity::whereId($id)->first();

        if(!$thatBuildingActivity)
            return response()->json(["level"=>"error","message"=>"该活动信息不存在！"]);

        $thatBuildingActivity->delete();

        return response()->json(["level"=>"success","message"=>"删除成功","data"=>$thatBuildingActivity]);

    }

    public function setBuildingProperty(Request $request){
        if(Auth::guard('admin')->guest())
            return response()->json(['level' => 'error','message' => '请先登录!']);

        $id                  = !is_null($request->get('id')) ? $request->get('id') : 0;
        $roomNum             = $request->get("room_num");
        $acreage             = $request->get("acreage");
        $remark              = $request->get("remark");
        $buildingId          = $request->get("building_id");

        /** @var BuildingProperty $thatBuildingProperty */
        $thatBuildingProperty = BuildingProperty::whereId($id)->first();

        if(!$thatBuildingProperty){
            $thatBuildingProperty = new BuildingProperty();
        }

        if(!is_null($roomNum))$thatBuildingProperty->room_num = $roomNum;
        if(!is_null($acreage))$thatBuildingProperty->acreage = $acreage;
        if(!is_null($remark))$thatBuildingProperty->remark = $remark;
        if(!is_null($buildingId))$thatBuildingProperty->building_id = $buildingId;
        $thatBuildingProperty->save();

        return response()->json(['level' => 'success','message' => '设置成功','data' => $thatBuildingProperty]);
    }

    public function delBuildingProperty(Request $request){
        if(Auth::guard('admin')->guest())
            return response()->json(['level' => 'error','message' => '请先登录!']);

        $id = !is_null($request->get('id'))?$request->get('id'):0;

        $thatBuildingProperty =BuildingProperty::whereId($id)->first();

        if(!$thatBuildingProperty)
            return response()->json(["level"=>"error","message"=>"该活动信息不存在！"]);

        $thatBuildingProperty->delete();

        return response()->json(["level"=>"success","message"=>"删除成功","data"=>$thatBuildingProperty]);

    }

    public function addClassify(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $title   = $request->get('title');
        $id = !is_null($request->get('id'))?$request->get('id'):0;

        $newClassify = Classify::whereId($id)->first();
        if($newClassify){
            $newClassify = Classify::whereId($id)->update(['name' => $title]);
        }else{
            $newClassify = Classify::insert(['name' => $title]);
        }

        return response()->json(["level"=>"success","message"=>"添加或修改成功","data"=>$newClassify]);
    }

    public function delClassify(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $id = !is_null($request->get('id'))?$request->get('id'):0;

        $thatClassify =Classify::whereId($id)->first();

        if(!$thatClassify)
            return response()->json(["level"=>"error","message"=>"该菜单不存在！"]);

        $thatClassify->delete();

        return response()->json(["level"=>"success","message"=>"删除成功","data"=>$thatClassify]);
    }

    public  function   setBuildingInfo(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $device_num   = $request->get('device_num');
        $build_num   = $request->get('build_num');
        $name   = $request->get('name');
        $desc   = $request->get('desc');
        $staff_num   = $request->get('staff_num');
        $party_member_num  = $request->get('party_member_num');
        $autogestion_num  = $request->get('autogestion_num');
        $flow_num   = $request->get('flow_num');
        $hatch_num  = $request->get('hatch_num');
        $coverage   = $request->get('coverage');

        if(!is_integer($staff_num)&& $staff_num<0 )
            return response()->json(['level'=>'error','message' => '请输入员工正确数量！']);
        if(!is_integer($party_member_num)&& $party_member_num<0 )
            return response()->json(['level'=>'error','message' => '请输入正确党员数量！']);
        if(!is_integer($autogestion_num)&& $autogestion_num<0 )
            return response()->json(['level'=>'error','message' => '请输入自管党员正确数量！']);
        if(!is_integer($flow_num)&& $flow_num<0 )
            return response()->json(['level'=>'error','message' => '请输入流动党员正确数量！']);
        if(!is_integer($hatch_num)&& $hatch_num<0 )
            return response()->json(['level'=>'error','message' => '请输入孵化正确数量！']);



        $id = !is_null($request->get('id'))?$request->get('id'):0;

        $newBuildingInfo=Building::whereId($id)->first();
        if(!$newBuildingInfo)
            $newBuildingInfo=new Building();
        if(!is_null($device_num))$newBuildingInfo->device_num=$device_num;
        if(!is_null($build_num))$newBuildingInfo->build_num=$build_num;
        if(!is_null($name))$newBuildingInfo->name=$name;
        if(!is_null($desc))$newBuildingInfo->desc=$desc;
        if(!is_null($staff_num))$newBuildingInfo->staff_num=$staff_num;
        if(!is_null($party_member_num))$newBuildingInfo->party_member_num=$party_member_num;
        if(!is_null($autogestion_num))$newBuildingInfo->autogestion_num=$autogestion_num;
        if(!is_null($flow_num))$newBuildingInfo->flow_num=$flow_num;
        if(!is_null($hatch_num))$newBuildingInfo->hatch_num=$hatch_num;
        $newBuildingInfo->coverage= (int)$party_member_num > 0 ? $flow_num/$party_member_num*100 ."%" : "-";

        $newBuildingInfo->save();

      return response()->json(["level"=>"success","message"=>"添加或修改成功","data"=>$newBuildingInfo]);

    }


    public function delBuildingInfo(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $id = !is_null($request->get('id'))?$request->get('id'):0;

        $newBuildingInfo=Building::whereId($id)->first();

        if(!$newBuildingInfo)
            return response()->json(['level'=>'error','message' => '找不到信息！']);


        $newBuildingInfo->delete();



        return response()->json(["level"=>"success","message"=>"删除成功","data"=>$newBuildingInfo]);
    }

    public  function  setBuildingCompany(Request $request){

        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $building_id=$request->get('building_id');
        $company_name=$request->get('company_name');
        $position=$request->get('position');
        $desc=$request->get('desc');
        $staff_num=$request->get('staff_num');
        $party_member_num=$request->get('party_member_num');
        $autogestion_num=$request->get('autogestion_num');
        $flow_num=$request->get('flow_num');


        if(is_integer($staff_num)|| $staff_num<0 )
            return response()->json(['level'=>'error','message' => '请输入员工正确数量！']);

        $id = !is_null($request->get('id'))?$request->get('id'):0;

        $buildingCompany=BuildingCompany::whereId($id)->first();

        if(!$buildingCompany)
            $buildingCompany =new  BuildingCompany();

        if(!is_null($building_id))$buildingCompany->building_id=$building_id;
        if(!is_null($company_name))$buildingCompany->company_name=$company_name;
        if(!is_null($position))$buildingCompany->position=$position;
        if(!is_null($desc))$buildingCompany->desc=$desc;
        if(!is_null($staff_num))$buildingCompany->staff_num=$staff_num;
        if(!is_null($party_member_num))$buildingCompany->party_member_num=$party_member_num;
        if(!is_null($autogestion_num))$buildingCompany->autogestion_num=$autogestion_num;
        if(!is_null($flow_num))$buildingCompany->flow_num=$flow_num;

        $buildingCompany->save();

        return response()->json(["level"=>"success","message"=>"设置成功","data"=>$buildingCompany]);
    }



    public  function  delBuildingCompany(Request $request){

        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $id = !is_null($request->get('id'))?$request->get('id'):0;

        $buildingCompany=BuildingCompany::whereId($id)->first();

        if(!$buildingCompany)
            return response()->json(['level'=>'error','message' => '找不到信息！']);


        $buildingCompany->delete();

        return response()->json(["level"=>"success","message"=>"删除成功","data"=>$buildingCompany]);
    }


    public  function  delPartyBranch(Request $request){

        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $id = !is_null($request->get('id'))?$request->get('id'):0;

        $partyBranch=BuildingPartyBranch::whereId($id)->first();

        if(!$partyBranch)
            return response()->json(['level'=>'error','message' => '找不到信息！']);


        $partyBranch->delete();

        return response()->json(["level"=>"success","message"=>"删除成功","data"=>$partyBranch]);
    }




    public  function  setPartyBranch(Request $request){

        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $id = !is_null($request->get('id'))?$request->get('id'):0;
        $desc=$request->get('desc');
        $higher_up_name=$request->get('higher_up_name');
        $build_time=$request->get('build_time');
        $company=$request->get('company');
        $building_id=$request->get('building_id');
        $name=$request->get('name');
        $secretary_name=$request->get('secretary_name');
        $content=$request->get('content_id');
        $party_member_num=$request->get('party_member_num');

        $partyBranch=BuildingPartyBranch::whereId($id)->first();

        if(!$partyBranch)
            $partyBranch=new  BuildingPartyBranch();
        if(!is_null($desc))$partyBranch->desc=$desc;
        if(!is_null($higher_up_name))$partyBranch->higher_up_name=$higher_up_name;
        if(!is_null($build_time))$partyBranch->build_time=$build_time;
        if(!is_null($company))$partyBranch->company=$company;
        if(!is_null($building_id))$partyBranch->building_id=$building_id;
        if(!is_null($name))$partyBranch->name=$name;
        if(!is_null($secretary_name))$partyBranch->secretary_name=$secretary_name;
        if(!is_null($content))$partyBranch->content="http://zddxapi.rzkeji.com/content?id=".$content;
        if(!is_null($party_member_num))$partyBranch->party_member_num=$party_member_num;
        $partyBranch->save();

        return response()->json(["level"=>"success","message"=>"设置成功","data"=>$partyBranch]);
    }




    public  function setUserDevice(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $id = !is_null($request->get('id'))?$request->get('id'):0;

        $device_id=$request->get('device_id');
        $user_id=$request->get('user_id');

        $userDevice=UserDevice::whereId($id)->first();
        if(!$userDevice)
            $userDevice=new UserDevice();
        if(!is_null($device_id))$userDevice->device_id=$device_id;
        $userDevice->user_id=$user_id;
        $userDevice->save();

        return response()->json(["level"=>"success","message"=>"设置成功","data"=>$userDevice]);
    }

    public  function  deleteUserDevice(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $id = !is_null($request->get('id'))?$request->get('id'):0;

        $userDevice=UserDevice::whereId($id)->first();

        if(!$userDevice)
            return response()->json(['level'=>'error','message' => '找不到信息！']);

        $userDevice->delete();

        return response()->json(["level"=>"success","message"=>"删除成功","data"=>$userDevice]);

    }

    public function addQuestionsPackage(Request $request){
        if(Auth::guard('admin')->guest())
            return response()->json(['level' => 'error','message' => '请先登录!']);

        $name     = $request->get('name');
        $desc     = $request->get('desc');
        $score    = $request->get('score');
        $deviceId = !is_null($request->get('device_id')) ? $request->get('device_id') : 0;
        $status   = $request->get('status');
        $id       = !is_null($request->get('id')) ? $request->get('id') : 0;

        /** @var SmallDevices $thisDevice */
        $thisDevice = SmallDevices::whereId($deviceId)->first();
        if(!$thisDevice)
            return response()->json(['level' => 'error','message' => '这个设备不存在']);

        $newQuestionsPackage = QuestionsPackage::whereId($id)->first();
        if(!$newQuestionsPackage)
            $newQuestionsPackage = new QuestionsPackage();

        if(!is_null($name))$newQuestionsPackage->name                     = $name;
        if(!is_null($desc))$newQuestionsPackage->desc                     = $desc;
        if(!is_null($score))$newQuestionsPackage->score                   = $score;
        if(!is_null($deviceId))$newQuestionsPackage->device_id            = $deviceId;
        if(!is_null($status))$newQuestionsPackage->status                 = $status;
        $newQuestionsPackage->device_num = $thisDevice->device_num;
        $newQuestionsPackage->save();

        return response()->json(['level' => 'success','message' => '设置成功','data' => $newQuestionsPackage]);
    }

    public function delQuestionsPackage(Request $request){
        if(Auth::guard('admin')->guest())
            return response()->json(['level' => 'error','message' => '请先登录!']);

        $id = !is_null($request->get('id')) ? $request->get('id') : 0;

        $thisQuestionsPackage = QuestionsPackage::whereId($id)->first();

        if(!$thisQuestionsPackage)
            return response()->json(['level'=>'error','message' => '找不到信息！']);

        $thisQuestionsPackage->delete();

        return response()->json(["level"=>"success","message"=>"删除成功","data"=>$thisQuestionsPackage]);
    }

    public function addQuestions(Request $request){
        if(Auth::guard('admin')->guest())
            return response()->json(['level' => 'error','message' => '请先登录!']);

        $question     = $request->get('question');
        $desc         = $request->get('desc');
        $score        = $request->get('score');
        $qpId         = !is_null($request->get('qp_id')) ? $request->get('qp_id') : 0;
        $id           = !is_null($request->get('id')) ? $request->get('id') : 0;

        /** @var QuestionsPackage $isQuestionsPackage */
        $isQuestionsPackage = QuestionsPackage::whereId($qpId)->first();
        if(!$isQuestionsPackage)
            return response()->json(['level' => 'error','message' => '题目所属的套题不存在']);

        $newQuestionsPackage = Questions::whereId($id)->first();
        if(!$newQuestionsPackage)
            $newQuestionsPackage = new Questions();

        if(!is_null($question))$newQuestionsPackage->question        = $question;
        if(!is_null($desc))$newQuestionsPackage->desc                = $desc;
        if(!is_null($score))$newQuestionsPackage->score              = $score;
        $newQuestionsPackage->qp_id            = $isQuestionsPackage->id;
        $newQuestionsPackage->save();

        return response()->json(['level' => 'success','message' => '设置成功','data' => $newQuestionsPackage]);
    }

    public function delQuestions(Request $request){
        if(Auth::guard('admin')->guest())
            return response()->json(['level' => 'error','message' => '请先登录!']);

        $id = !is_null($request->get('id')) ? $request->get('id') : 0;

        $thisQuestions = Questions::whereId($id)->first();

        if(!$thisQuestions)
            return response()->json(['level'=>'error','message' => '找不到信息！']);

        $thisQuestions->delete();

        return response()->json(["level"=>"success","message"=>"删除成功","data"=>$thisQuestions]);
    }

    public function addQuestionsAnswers(Request $request){
        if(Auth::guard('admin')->guest())
            return response()->json(['level' => 'error','message' => '请先登录!']);

        $num                  = $request->get('num');
        $answer               = $request->get('answer');
        $isRightAnswer        = $request->get('is_right_answer');
        $qId                  = !is_null($request->get('q_id')) ? $request->get('q_id') : 0;
        $id                   = !is_null($request->get('id')) ? $request->get('id') : 0;

        /** @var Questions $isQuestions */
        $isQuestions = Questions::whereId($qId)->first();
        if(!$isQuestions)
            return response()->json(['level' => 'error','message' => '题目不存在']);

        $newQuestionsAnswers = QuestionsAnswers::whereId($id)->first();
        if(!$newQuestionsAnswers)
            $newQuestionsAnswers = new QuestionsAnswers();

        if(!is_null($num))$newQuestionsAnswers->num                           = $num;
        if(!is_null($answer))$newQuestionsAnswers->answer                     = $answer;
        if(!is_null($isRightAnswer))$newQuestionsAnswers->is_right_answer     = $isRightAnswer;
        $newQuestionsAnswers->q_id            = $isQuestions->id;
        $newQuestionsAnswers->save();

        return response()->json(['level' => 'success','message' => '设置成功','data' => $newQuestionsAnswers]);
    }

    public function delQuestionsAnswers(Request $request){
        if(Auth::guard('admin')->guest())
            return response()->json(['level' => 'error','message' => '请先登录!']);

        $id = !is_null($request->get('id')) ? $request->get('id') : 0;

        $thisQuestionsAnswers = QuestionsAnswers::whereId($id)->first();

        if(!$thisQuestionsAnswers)
            return response()->json(['level'=>'error','message' => '找不到信息！']);

        $thisQuestionsAnswers->delete();

        return response()->json(["level"=>"success","message"=>"删除成功","data"=>$thisQuestionsAnswers]);
    }

    public function setVideo(Request $request){
        if(Auth::guard('admin')->guest())
            return response()->json(['level' => 'error','message' => '请先登录!']);

        $id       = !is_null($request->get('id')) ? $request->get('id') : 0;
        $title    = $request->get("title");
        $form     = $request->get("form");
        $from     = $request->get("from");
        $type     = $request->get("type");
        $videoUrl = $request->get("video_url");
        $videoModel = $request->get("video_model");
        $linkUrl  = $request->get("link_url");
        $question = $request->get("question");
        $desc     = $request->get("desc");

        $theVideo = VideoModel::whereId($id)->first();
        if(!$theVideo){
            $theVideo = new VideoModel();
        }

        if(!is_null($title))$theVideo->title = $title;
        if(!is_null($form))$theVideo->form = $form;
        if(!is_null($from))$theVideo->from = $from;
        if(!is_null($type))$theVideo->type = $type;
        if(!is_null($linkUrl))$theVideo->link_url = $linkUrl;
        if(!is_null($videoModel))$theVideo->model = $videoModel;
        if(!is_null($question))$theVideo->question = $question;
        if(!is_null($desc))$theVideo->desc = $desc;
        $theVideo->num = 1;
        $theVideo->save();

        $thatVideoData = VideoData::whereVideoModelId($theVideo->id)->first();
        if(!$thatVideoData){
            $thatVideoData = new VideoData();
            $thatVideoData->video_model_id = $theVideo->id;
        }

        if(!is_null($videoUrl))$thatVideoData->hls_url = $videoUrl;
        $thatVideoData->save();

        return response()->json(["level"=>"success","message"=>"设置或修改成功","data"=>$theVideo]);
    }

    public function delVideo(Request $request){
        if(Auth::guard('admin')->guest())
            return response()->json(['level' => 'error','message' => '请先登录!']);

        $id = !is_null($request->get('id')) ? $request->get('id') : 0;

        $thatVideo = VideoModel::whereId($id)->first();

        if(!$thatVideo)
            return response()->json(['level'=>'error','message' => '找不到信息！']);

        $thatVideo->delete();

        return response()->json(["level"=>"success","message"=>"删除成功","data"=>$thatVideo]);
    }

    public function getVideoInfo(Request $request){
        if(Auth::guard('admin')->guest())
            return response()->json(['level' => 'error','message' => '请先登录!']);

        $id = !is_null($request->get('id')) ? $request->get('id') : 0;

        $thatVideo = VideoModel::whereId($id)->first();

        if(!$thatVideo)
            return response()->json(['level'=>'error','message' => '找不到信息！']);

        return response()->json(["level"=>"success","message"=>"删除成功","data"=>$thatVideo]);
    }

    public function setSmallVideo(Request $request){
        if(Auth::guard('admin')->guest())
            return response()->json(['level' => 'error','message' => '请先登录!']);

        $id       = !is_null($request->get('id')) ? $request->get('id') : 0;
        $title    = $request->get("title");
        $videoUrl = $request->get("video_url");
        $desc     = $request->get("desc");

        $theVideo = SmallVideo::whereId($id)->first();
        if(!$theVideo){
            $theVideo = new SmallVideo();
        }

        if(!is_null($title))$theVideo->title = $title;
        if(!is_null($desc))$theVideo->desc = $desc;
        if(!is_null($videoUrl))$theVideo->video_url = $videoUrl;
        $theVideo->save();

        return response()->json(["level"=>"success","message"=>"设置或修改成功","data"=>$theVideo]);
    }

    public function delSmallVideo(Request $request){
        if(Auth::guard('admin')->guest())
            return response()->json(['level' => 'error','message' => '请先登录!']);

        $id = !is_null($request->get('id')) ? $request->get('id') : 0;

        $thatVideo = SmallVideo::whereId($id)->first();

        if(!$thatVideo)
            return response()->json(['level'=>'error','message' => '找不到信息！']);

        $thatVideo->delete();

        return response()->json(["level"=>"success","message"=>"删除成功","data"=>$thatVideo]);
    }

    public function getSmallVideoInfo(Request $request){
        if(Auth::guard('admin')->guest())
            return response()->json(['level' => 'error','message' => '请先登录!']);

        $id = !is_null($request->get('id')) ? $request->get('id') : 0;

        $thatVideo = SmallVideo::whereId($id)->first();

        if(!$thatVideo)
            return response()->json(['level'=>'error','message' => '找不到信息！']);

        return response()->json(["level"=>"success","message"=>"删除成功","data"=>$thatVideo]);
    }

    public function setNews(Request $request){
        if(Auth::guard('admin')->guest())
            return response()->json(['level' => 'error','message' => '请先登录!']);

        $id           = !is_null($request->get('id')) ? $request->get('id') : 0;
        $title        = $request->get("title");
        $from         = $request->get("from");
        $fromUrl      = $request->get("from_url");
        $type         = $request->get("type");
        $content      = $request->get("content");
        $isShow       = $request->get('is_show');
        $videoUrl     = $request->get("video_url");
        $newsTime     = $request->get("news_time");
        $sort         = $request->get('sort');

        $newNews = News::whereId($id)->first();
        if(!$newNews){
            $newNews = new News();
        }

        if(!is_null($title))$newNews->title               = $title;
        if(!is_null($from))$newNews->from                 = $from;
        if(!is_null($fromUrl))$newNews->from_url          = $fromUrl;
        if(!is_null($type))$newNews->type                 = $type;
        if(!is_null($content))$newNews->content           = $content;
        if(!is_null($isShow))$newNews->is_show            = $isShow;
        if(!is_null($videoUrl))$newNews->video_url        = $videoUrl;
        if(!is_null($newsTime))$newNews->news_time        = $newsTime;
        if(!is_null($sort))$newNews->sort                 = $sort;
        $newNews->save();

        return response()->json(["level"=>"success","message"=>"设置或修改成功","data"=>$newNews]);
    }

    public function changeNewsShow(Request $request){
        if(Auth::guard('admin')->guest())
            return response()->json(['level' => 'error','message' => '请先登录!']);

        $id           = !is_null($request->get('id')) ? $request->get('id') : 0;

        /** @var News $thatNews */
        $thatNews = News::whereId($id)->first();
        if(!$thatNews)
            return response()->json(['level' => 'error','message' => '该信息不存在']);

        if($thatNews->is_show == 1){
            $thatNews->is_show = 0;
            $thatNews->save();
        }else{
            $thatNews->is_show = 1;
            $thatNews->save();
        }

        return response()->json(['level' => 'success','message' => '设置成功','data' => $thatNews]);
    }

    public function setRedInfo(Request $request){
        if(Auth::guard('admin')->guest())
            return response()->json(['level' => 'error','message' => '请先登录!']);

        $id             = !is_null($request->get('id')) ? $request->get('id') : 0;
        $title          = $request->get("title");
        $desc           = $request->get("desc");
        $content        = $request->get("content");
        $thumbId        = !is_null($request->get("thumb_id")) ? $request->get("thumb_id") : 0;
        $type           = $request->get('type');
        $linkUrl        = $request->get('link_url');
        $imgString      = $request->get('imgString');

        $newRedInfo = RedInfo::whereId($id)->first();
        if(!$newRedInfo)
            $newRedInfo = new RedInfo();

        if(!is_null($title))$newRedInfo->title                = $title;
        if(!is_null($desc))$newRedInfo->desc                  = $desc;
        if(!is_null($content))$newRedInfo->content            = $content;
        $newRedInfo->thumb_id                                 = $thumbId;
        if(!is_null($desc))$newRedInfo->type                  = $type;
        if(!is_null($linkUrl))$newRedInfo->link_url           = $linkUrl;
        $newRedInfo->save();

        if(!is_null($imgString) && $imgString != ''){
            /** @var RedInfoImgs $redInfoImgs */
            $redInfoImgsImgs = RedInfoImgs::whereRedInfoId($newRedInfo->id)->get();
            if(count($redInfoImgsImgs) > 0){
                foreach($redInfoImgsImgs as $redInfoImgs){
                    $redInfoImgs->forceDelete();
                }
            }

            $imgIds = explode(',',$imgString);
            if(count($imgIds) > 0){
                foreach($imgIds as $imgId){
                    $newRedInfoImg = new RedInfoImgs();
                    $newRedInfoImg->img_id = $imgId;
                    $newRedInfoImg->red_info_id = $newRedInfo->id;
                    $newRedInfoImg->save();
                }
            }
        }

        return response()->json(['level' => 'success','message' => '设置成功','data' => $newRedInfo]);
    }

    public function delRedInfo(Request $request){
        if(Auth::guard('admin')->guest())
            return response()->json(['level' => 'error','message' => '请先登录!']);

        $id = !is_null($request->get('id')) ? $request->get('id') : 0;

        $thatRedInfo = RedInfo::whereId($id)->first();

        if(!$thatRedInfo)
            return response()->json(['level'=>'error','message' => '找不到信息！']);

        $thatRedInfo->delete();

        return response()->json(["level"=>"success","message"=>"删除成功","data"=>$thatRedInfo]);
    }
}